<?php
/**
 * Created by PhpStorm.
 * User: Dinh Thi
 * Date: 2/24/15
 * Time: 10:32 PM
 */

namespace MyApp\Application\Controller;


use MyApp\Application\Model\ChiTieuModel;
use MyApp\Application\Model\HoaDonModel;
use MyApp\Application\Model\MatHangModel;
use MyApp\Application\Model\StaticModel;

class ThongKeController extends MyAppAbstractController
{
    public function __construct()
    {
        parent::__construct();
        parent::mustBeLogin();
    }

    public function indexAction()
    {
        $this->view->setTitle('Thống kê');
        $this->view->render('ThongKe/index');
    }

    public function DoanhSoAction()
    {
        $this->view->addHeaderScript('Public/plugin/chart/js/highcharts.js');
        $this->view->addHeaderScript('Public/js/ThongKe/DoanhSo.js');

        $this->view->setTitle('Doanh số');
        $this->view->render('ThongKe/doanh-so');
    }


    public function HangTonKhoAction(){
        $this->view->addHeaderScript('Public/plugin/autoNumberic/autoNumberic.js');

        $this->view->addHeaderScript('Public/js/ThongKe/TonKho.js');
        $this->view->setTitle('Hàng tồn kho');
        $this->view->render('ThongKe/ton-kho');
    }


    public function CongNoAction(){
        $this->view->addHeaderScript('Public/plugin/autoNumberic/autoNumberic.js');

        $this->view->addHeaderScript('Public/js/ThongKe/CongNo.js');
        $this->view->setTitle('Công nợ');
        $this->view->render('ThongKe/cong-no');
    }


    public function CongNoMuaHangAction(){
        $this->view->addHeaderScript('Public/plugin/autoNumberic/autoNumberic.js');

        $this->view->addHeaderScript('Public/js/ThongKe/CongNoMuaHang.js');
        $this->view->setTitle('Công nợ mua hàng');
        $this->view->render('ThongKe/mua-hang');
    }

    /*=============Ajax============*/

    public function ThongKeDoanhSoTheoNgayAction()
    {
        if (isset ($_POST['begin']) && isset ($_POST['end'])) {
            $begin = $_POST['begin'];
            $end = $_POST['end'];

            $begin = str_replace('/', '-', $begin);
            $begin = date('Y-m-d', strtotime($begin));

            $end = str_replace('/', '-', $end);
            $end = date('Y-m-d', strtotime($end));

            $listNgay = StaticModel::dateRange($begin, $end);

            $begin .= ' 00:00:00';
            $end .= ' 23:59:59';

            $hoadon = new HoaDonModel();
            $chitieu = new ChiTieuModel();

            $listDoanhSo = $hoadon->DoanhSoTheoNgay($begin, $end);
            $listChiTieu = $chitieu->ChiTieuTheoNgay($begin, $end);


            $data = array(
                'Ngay' => array(),
                'GiaGoc' => array(),
                'TriGia' => array(),
                'ChiTieu' => array(),
                'LoiNhuan' => array()
            );
            foreach ($listNgay as $value) {
                $data['Ngay'][] = $value;

                $giagoc = 0;
                $trigia = 0;
                $chitieu = 0;

                $arr = array_filter($listDoanhSo, function ($obj) use ($value) {
                    return $value == $obj['Ngay'];
                });
                $arr = array_merge($arr);
                if (!empty($arr[0])) {
                    $giagoc = (float)$arr[0]['GiaGoc'];
                    $trigia = (float)$arr[0]['TriGia'];
                }

                $arr = array_filter($listChiTieu, function ($obj) use ($value) {
                    return $value == $obj['Ngay'];
                });
                $arr = array_merge($arr);
                if (!empty($arr[0])) {
                    $chitieu = (float)$arr[0]['TongChi'];
                }

                $data['GiaGoc'][] = $giagoc;

                $data['TriGia'][] = $trigia;

                $data['ChiTieu'][] = $chitieu;

                $data['LoiNhuan'][] = $trigia - $giagoc - $chitieu;
            }

            echo json_encode($data);
        }


    }

    public function ThongKeDoanhSoTheoThangAction()
    {

        if (isset ($_POST['begin']) && isset ($_POST['end'])) {
            $begin = $_POST['begin'];
            $end = $_POST['end'];

            $begin = str_replace('/', '-', $begin);
            $begin = date('Y-m-d', strtotime($begin));

            $end = str_replace('/', '-', $end);
            $end = date('Y-m-d', strtotime($end));


            $listMonth = StaticModel::dateRange($begin, $end, '+1 month', 'm/Y');


            $chitieu = new ChiTieuModel();
            $listChiTieu = $chitieu->ChiTieuTheoThang($begin, $end);

            $hoadon = new HoaDonModel();
            $listDoanhSo = $hoadon->DoanhSoTheoThang($begin, $end);
            /*
                    print_r($listChiTieu);
                    print_r($listDoanhSo);*/

            $data = array(
                'Ngay' => array(),
                'GiaGoc' => array(),
                'TriGia' => array(),
                'ChiTieu' => array(),
                'LoiNhuan' => array()
            );
            foreach ($listMonth as $value) {
                $data['Ngay'][] = $value;

                $giagoc = 0;
                $trigia = 0;
                $chitieu = 0;

                $arr = array_filter($listDoanhSo, function ($obj) use ($value) {
                    return $value == $obj['Ngay'];
                });
                $arr = array_merge($arr);
                if (!empty($arr[0])) {
                    $giagoc = (float)$arr[0]['GiaGoc'];
                    $trigia = (float)$arr[0]['TriGia'];
                }

                $arr = array_filter($listChiTieu, function ($obj) use ($value) {
                    return $value == $obj['Ngay'];
                });
                $arr = array_merge($arr);
                if (!empty($arr[0])) {
                    $chitieu = (float)$arr[0]['TongChi'];
                }

                $data['GiaGoc'][] = $giagoc;

                $data['TriGia'][] = $trigia;

                $data['ChiTieu'][] = $chitieu;

                $data['LoiNhuan'][] = $trigia - $giagoc - $chitieu;
            }

            echo json_encode($data);
        }
    }

} 